package cn.sciento.farm.devicemanager.gateway.uplink.service;

import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class DeviceMessageService {

    @Autowired
    Map<String,DataAdapter> dataAdapterMap;


    public void handle(String topic, Object message) {
        //分割topic
        String[] subTopic = topic.split("/");

        if (topic.length() < 2) {
            return;
        }
//        log.info("入口   topic  {}  msg   {}",topic,message.toString());
    String protocol = subTopic[0];
    String serviceName = MqttConstant.PROTOCOL_PREFIX + protocol;
    DataAdapter dataAdapter = dataAdapterMap.get(serviceName);
    if (dataAdapter != null) {
        dataAdapter.handle(subTopic, message);
    } else {
        log.warn("没有相关的类型协议topic：" + topic);
    }
}
}
